<template>
  <el-dialog title="展示到达预报"
             :visible.sync="dialogForm"
             width="1200px">
    <h3>验收物资明细</h3>
    <el-table :data="form.acceptList"
              border
              stripe
              show-summary
              :summary-method="getAcceptSummaries"
              max-height="550">
      <el-table-column label="序号"
                       type="index"
                       width="50"></el-table-column>
      <!-- <el-table-column prop="trackingNo"
                       label="跟踪号"
                       min-width="100"></el-table-column> -->
      <el-table-column prop="stockBillNo"
                       label="存单号"
                       min-width="100"></el-table-column>
      <el-table-column prop="checkAcceptNo"
                       label="验收单号"
                       min-width="100"></el-table-column>
      <el-table-column prop="cargoName"
                       label="品名"
                       min-width="100"></el-table-column>
      <el-table-column prop="normStr"
                       label="规格"
                       min-width="100"></el-table-column>
      <el-table-column prop="material"
                       label="材质"
                       min-width="100"></el-table-column>
      <el-table-column prop="madeBy"
                       label="生产厂家"
                       min-width="100"></el-table-column>
      <el-table-column prop="baleNo"
                       label="捆包号"
                       min-width="100"></el-table-column>
      <el-table-column prop="checkAcceptNum"
                       label="验收件数"
                       min-width="120"></el-table-column>
      <el-table-column prop="checkAcceptWeight"
                       label="验收重量"
                       min-width="100"></el-table-column>
    </el-table>
    <h3>到达预报明细</h3>
    <el-table :data="form.entrustList"
              border
              stripe
              show-summary
              :summary-method="getEntrustSummaries"
              max-height="550">
      <el-table-column label="序号"
                       type="index"
                       width="50"></el-table-column>
      <!-- <el-table-column prop="trackingNo"
                       label="跟踪号"
                       min-width="100"></el-table-column> -->
      <el-table-column prop="cargoName"
                       label="品名"
                       min-width="100"></el-table-column>
      <el-table-column prop="normStr"
                       label="规格"
                       min-width="100"></el-table-column>
      <el-table-column prop="material"
                       label="材质"
                       min-width="100"></el-table-column>
      <el-table-column prop="madeBy"
                       label="生产厂家"
                       min-width="100"></el-table-column>
      <el-table-column prop="baleNo"
                       label="捆包号"
                       min-width="100"></el-table-column>
      <el-table-column prop="num"
                       label="预报件数"
                       min-width="120"></el-table-column>
      <el-table-column prop="weight"
                       label="预报重量"
                       min-width="100"></el-table-column>
      <el-table-column prop="accountNum"
                       label="上账件数"
                       min-width="100"></el-table-column>
      <el-table-column prop="accountWeight"
                       label="上账重量"
                       min-width="100"></el-table-column>
      <el-table-column prop="surplusNum"
                       label="剩余数量"
                       min-width="100"></el-table-column>
      <el-table-column prop="surplusWeight"
                       label="剩余重量"
                       min-width="100"></el-table-column>
    </el-table>
    <h3>剩余重量与验收重量相差：{{ form.diffAcceptWeight }}</h3>
    <h3>剩余重量与预上账重量相差：{{ form.diffAccountWeight }}</h3>
    <div slot="footer"
         class="dialog-footer">
      <el-button type="primary"
                 @click="addAccount">上 账</el-button>
      <el-button @click="dialogForm = false">返 回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAccount } from '@/api/enter/depositReceipt'

export default {
  name: 'showArrivalForecastForm',
  data() {
    return {
      form: this.clearForm(),
      dialogForm: false,
      data: []
    }
  },
  methods: {
    clearForm() {
      return {}
    },
    //上账
    addAccount() {
      addAccount(this.form).then(res => {
        this.$message({ message: '上账成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    //合计
    getAcceptSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (
          column.property === 'checkAcceptNum' ||
          column.property === 'checkAcceptWeight'
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index]
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    //合计
    getEntrustSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (
          column.property === 'num' ||
          column.property === 'weight' ||
          column.property === 'surplusNum' ||
          column.property === 'surplusWeight'
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index]
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-height {
  margin: 10px 0;
}
h3 {
  margin: 10px 0;
}
.labels {
  margin-right: 5px;
}
</style>
